package com.youxin.cpcenter.controller;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.youxin.busfoundation.Consts;
import com.youxin.busfoundation.bean.ResponseObject;
import com.youxin.busfoundation.common.redis.RedisOperator;
import com.youxin.busfoundation.enums.OrderPayTypeEnums;
import com.youxin.busfoundation.enums.ProductsCouponEnums;
import com.youxin.cpcenter.bean.RequestOrderInfoBean;
import com.youxin.cpcenter.bean.RequestTradeQueryBean;
import com.youxin.cpcenter.common.log.LogMethod;
import com.youxin.cpcenter.model.TbOrderInfo;
import com.youxin.cpcenter.service.AliPayService;
import com.youxin.cpcenter.service.TransactionService;
import com.youxin.foundation.utils.ObjectUtils;
/**
 * 
 * @ClassName: AliPayController 
 * @Description: 支付宝支付  
 * @author yingde.cao
 * @date 2017年11月24日 下午5:55:07 
 *
 */
@RestController
public class AliPayController extends BaseController{
	private static final Logger logger = LoggerFactory.getLogger(AliPayController.class);
	@Autowired
	private AliPayService aliPayService;
	@Autowired
	private TransactionService transactionService;

	/**
	 * @Title: pcPay 
	 * @Description: 电脑支付
	 * @param @param product
	 * @param @param map
	 * @param @return
	 * @return String
	 * @throws
	 */
	@LogMethod(id="ent.api.alipay.pcpay",eventContent={})
	@RequestMapping(value="/api/alipay/pcpay",method=RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseObject pcPay(HttpServletRequest request, HttpServletResponse httpServletResponse,
			@RequestBody RequestOrderInfoBean requestBean) {
		try {
			ObjectUtils.requestNonEmpty(requestBean.getOrderType().toString(), "订单类型为空");
			//orderType类型为 充值金券 产品ID为必传
			if(OrderPayTypeEnums.CZJJ.getIndex() == requestBean.getOrderType()) {
				ObjectUtils.requestNonEmpty(requestBean.getProductId().toString(), "产品ID为空");
				ObjectUtils.requestNonEmpty(ProductsCouponEnums.getName(requestBean.getProductId()), "产品ID不正确");
			}
			ObjectUtils.requestNonEmpty(requestBean.getFrontUrl(), "支付宝前端返回地址为空");
			return aliPayService.aliPayPc(requestBean);
		} catch (Exception e) {
			return ResponseObject.systemError("/api/alipay/pcpay异常"+e);
		}
    }
	
	@RequestMapping(value="/ent/alipay/pay/{id}",method=RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
    public void invokePay(HttpServletRequest request, HttpServletResponse httpServletResponse,
			@PathVariable long id) {
		try {
			String form=RedisOperator.getAliPay(id);	
			httpServletResponse.setHeader("Content-type", "text/html;charset=UTF-8");  
			httpServletResponse.setContentType("text/html"); 
			httpServletResponse.getWriter().write(String.valueOf(form));			
		} catch (Exception e) {
			
		}
    }
	
	/**
	 * @Title: alipay_notify 
	 * @Description: 支付宝支付回调(二维码、H5、网站)
	 * @param @param request
	 * @param @param response
	 * @param @throws Exception
	 * @return void
	 * @throws
	 */
	/*@RequestMapping(value="/api/alipay/return",method=RequestMethod.GET)
	public ResponseObject alipayReturn(HttpServletRequest request, HttpServletResponse response) throws Exception {
		logger.info("支付宝支付返回前端页面");
		try {
			return aliPayService.aliPayReturn(request);
		} catch (Exception e) {
			logger.error("/api/alipay/return异常", e);
			return ResponseObject.systemError(e);
		}
	}*/
   /**
    * @Title: alipay_notify 
    * @Description: 支付宝支付回调(二维码、H5、网站)
    * @param @param request
    * @param @param response
    * @param @throws Exception
    * @return void
    * @throws
    */
    @LogMethod(id="ent.api.alipay.notify",eventContent={})
	@RequestMapping(value="/api/alipay/notify",method=RequestMethod.POST)
	public String alipayNotify(HttpServletRequest request, HttpServletResponse response) throws Exception {
		logger.warn("支付宝支付回调");
		String rs = Consts.FAIL_LOWER;
		try {
			rs = aliPayService.aliPayNotify(request);
		} catch (Exception e) {
			logger.error("/api/alipay/notify异常", e);
			return rs;
		}
		return rs;
	}
	/**
	 * @Title: alipayTradeQuery 
	 * @Description: 支付宝交易结果查询
	 * @param @param request
	 * @param @param httpServletResponse
	 * @param @param requestBean
	 * @param @return
	 * @return ResponseObject
	 * @throws
	 */
	@LogMethod(id="ent.api.alipay.trade.query",eventContent={})
	@RequestMapping(value="/api/alipay/trade/query",method=RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseObject alipayTradeQuery(HttpServletRequest request, HttpServletResponse httpServletResponse,
			@RequestBody RequestTradeQueryBean requestBean) {
		try {
			ObjectUtils.requestNonEmpty(requestBean.getOrderSn(), "商户订单号为空");
			TbOrderInfo model = transactionService.queryAlipay(requestBean.getOrderSn());
			return transactionService.procesResponseStatus(model);
		} catch (Exception e) {
			return ResponseObject.systemError("/api/alipay/tradeQuery异常"+e);
		}
    }
	
}
